/*
 * PortDescription.java
 * 
 * last update: 24.01.2010 by Olaru Victor
 * 
 * author:	Victor(victor.olaru@gmail.com)
 * 
 * Obs:
 */

package engine;

import middle.*;
//import java.util.*;

@SuppressWarnings("serial")
class PortDescription extends Symbol{

	public static final int none    = 0x0000,
	input   = 0x0001,
	output  = 0x0002,
	inout   = input | output;
	int type;

	ExpressionDescription msbExp, lsbExp;
	int nMsb, nLsb;

	public PortDescription(String name, int lineNo, int type,
			ExpressionDescription msb,
			ExpressionDescription lsb){
		super(name, lineNo);
		this.type = type;
		this.msbExp = msb;
		this.lsbExp = lsb;
	}

	public PortDescription(String name, int lineNo, int type){
		this(name, lineNo, type, null, null);
	}

	public PortDescription(String name){
		this(name, 0, none, null, null);
	}

	void setPortType(int type)throws ParseException{
		this.type = type;
	}

	void setLine(int lineNo){
		this.lineNo = lineNo;
	}

	void setRange(ExpressionDescription msb, ExpressionDescription lsb){
		this.lsbExp = lsb;
		this.msbExp = msb;
	}

	/**
	 * Computes the actual msb & lsb for this port
	 * @param thisScope may safely be null
	 */
	 Object instantiate(NameSpace ns, ScopeNode thisScope)
	 throws ParseException{
		 nMsb = nLsb = 0;
		 if(msbExp != null){
			 try{
				 nMsb = (int) msbExp.instantiate(ns).evaluate().getLong();
				 nLsb = (int) lsbExp.instantiate(ns).evaluate().getLong();
			 }catch(InterpretTimeException iex){
				 throw new ParseException(((DefaultNameSpaceDescription)ns.desc).
						 toString(lineNo) +
						 " error : bad port daclaration :" + iex); 
			 }
		 }
		 return null;
	 }

	 public int getType(){
		 return portType;
	 }

	 public void checkRange(long msb, long lsb)throws ParseException{
		 if ((msb != nMsb) && (lsb != nLsb))
			 throw new ParseException(" error : port ranges don't match :");
	 }

	 public String toString(){
		 return name;
	 }
}
